package com.baowenli.server;


import com.baowenli.mapper.WorkerMapper;
import com.baowenli.pojo.Worker;
import com.baowenli.util.SqlSessionFactoryUtils;


import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class WorkerService {
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    /**
     * 查询所有
     *
     * @return
     */
    public List<Worker> selectAll() {
        //2.获取sqlSession
        SqlSession sqlSession = factory.openSession();
        //3.获取BrandMapper
        WorkerMapper mapper = sqlSession.getMapper(WorkerMapper.class);
        //4.调用方法
        List<Worker> brands = mapper.selectAll();
        sqlSession.commit();
        sqlSession.close();
        return brands;
    }

    /**
     * 进行操作员登录
     *
     * @return
     */
    public Worker selectById(String username,String password) {
        //2.获取sqlSession
        SqlSession sqlSession = factory.openSession();
        //3.获取BrandMapper
        WorkerMapper mapper = sqlSession.getMapper(WorkerMapper.class);
        //4.调用方法
        Worker work = mapper.selectById(username,password);
        sqlSession.commit();
        sqlSession.close();
        return work;
    }

    /**
     * 进行管理员登录
     *
     * @return
     */
    public Worker selectByIdManget(String username,String password) {
        //2.获取sqlSession
        SqlSession sqlSession = factory.openSession();
        //3.获取BrandMapper
        WorkerMapper mapper = sqlSession.getMapper(WorkerMapper.class);
        //4.调用方法
        Worker work = mapper.selectByIdMange(username,password);
        sqlSession.commit();
        sqlSession.close();
        return work;
    }

    /**
     * 进行解雇操作员
     *
     * @return
     */
    public int reliveWorker(String username,String password) {
        //2.获取sqlSession
        SqlSession sqlSession = factory.openSession();
        //3.获取BrandMapper
        WorkerMapper mapper = sqlSession.getMapper(WorkerMapper.class);
        //4.调用方法
        int temp = mapper.DeleteWorker(username,password);
        sqlSession.commit();
        sqlSession.close();
        return temp;
    }

    /**
     * 进行解雇操作员
     *
     * @return
     */
    public int workerPromote(String username,String password) {
        //2.获取sqlSession
        SqlSession sqlSession = factory.openSession();
        //3.获取BrandMapper
        WorkerMapper mapper = sqlSession.getMapper(WorkerMapper.class);
        //4.调用方法
        int temp = mapper.workerPromote(username,password);
        sqlSession.commit();
        sqlSession.close();
        return temp;
    }

}
